/*
 * LogisticRegHelper.cpp
 *
 *  Created on: Dec 23, 2009
 *      Author: tunpixel
 */

#include "LogisticRegHelper.h"
#include "../common/Common.h"

namespace bouguerra {

LogisticRegHelper::LogisticRegHelper() {
	maxIndex = 0;
}

LogisticRegHelper::~LogisticRegHelper() {
	// TODO delete thetas
}


void LogisticRegHelper::loadThetasFromFile(CvMat* thetas, const char* filename) {
	ifstream in(filename);
	char str[255];
	for (int i = 0; i < 4; i++) {
		in.getline(str, 255);
		cvmSet(thetas, 0, i, atof(str));
	}
	in.close();
}


void LogisticRegHelper::saveThetasToFile(CvMat* thetas , const char* filename) {
	ofstream out(filename);

	for (int i = 0; i < 4; i++) {
		float theta = cvmGet(thetas, 0, i);
		out << theta << endl;

	}
	out.close();

}

/**
 * loads the list of the images the will be used for trainning.
 * the list should look like:
 * source_image_path.jpg source_image_labled_path.jpg
 */
void  LogisticRegHelper::loadFileList(const char* fileName) {
	char* image = NULL;
	char* image_y = NULL;

	printf("TRAINING ... Processing file: %s\n", fileName);
	FILE* file = fopen(fileName, "rt");
	if (!file) {
		printf("Can't open %s\n", fileName);
		return;
	}
	char buf[1000 + 1];
	int line = 0;

	while (fgets(buf, 1000, file)) {
		line++;
		image = strtok(buf, " ");
		image_y = strtok(NULL, " \n");
		if (!image || !image_y) {
			printf("Error line %d\n", line);
		} else if (strlen(image) >= NAME_SIZE || strlen(image_y) >= NAME_SIZE) {
			printf("Line:  %d, Name too long >= %d\n", line, NAME_SIZE);
		} else {
			printf("IMG: %s, IMG_y: %s\n", image, image_y);
			strncpy(originalImages[maxIndex], image, NAME_SIZE);
			strncpy(maskImages[maxIndex], image_y, NAME_SIZE);
			maxIndex++;
			if (maxIndex == MAX_FILE_LIST)
				break;
		}
	}
	fclose(file);

}

}
